package com.youdianstar.app.common.util;


import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Formatter;
import java.util.GregorianCalendar;
import java.util.Locale;
import java.util.TimeZone;

/**
 * File: DateUtil
 * Author: 82149 Create: 2019/3/15 18:06
 * Changes (from 2019/3/15)
 * --------------------------------------------------
 * describe:
 * ---------------------------------------------------
 */
public class DateUtil {
    /**
     * 时间日期格式化到年月日时分秒.
     */
    public static final String DATEFORMATYMDHMS = "yyyy-MM-dd HH:mm:ss";

    /**
     * 时间日期格式化到年月日时分.
     */
    public static final String FORMAT_YMDHM = "yyyy-MM-dd HH:mm";

    /**
     * 时间日期格式化到年月日.
     */
    public static final String FORMAT_YMD = "yyyy-MM-dd";

    /**
     * 时间日期格式化到年月.
     */
    public static final String FORMAT_YM = "yyyy-MM";

    /**
     * 时间日期格式化到月日.
     */
    public static final String FORMAT_MD = "MM-dd";

    /**
     * 时分秒.
     */
    public static final String FORMAT_HMS = "HH:mm:ss";

    /**
     * 星期
     */
    public static final String EEEE = "EEEE";


    /**
     * 英文格式 斜杠分隔
     */
    public static final String DMY_BRS = "dd/MM/yyyy";
    /**
     * 英文格式 斜杠分隔 : 兼容错误
     */
    public static final String MDY_BRS = "MM/dd/yyyy";

    /**
     * 10 Apr,2020 16:49:21
     */
    public static final String TIME_DMYHMS_ORDER = "dd MMM, yyyy HH:mm:ss";

    /**
     * 10 Apr,2020
     */
    public static final String DATE_DMY_ORDER = "dd MMM, yyyy";

    /**
     * 10 Apr
     */
    public static final String DM_ORDER = "dd MMM";


    /**
     * 英文格式  星期，月/日，时间
     */
    public static final String YFMDHM = "EEE, MMM dd, HH:mm";

    /**
     * 英文格式 月/日 年 时分秒
     */
    public static final String MDYHMS = "EEE,MMM dd yyyy, HH:mm:ss";


    public static String getOrderTime(Long time) {
        if (time == null || time == 0L) return " ";
        return getProjectTime(time, TIME_DMYHMS_ORDER);
    }

    public static String getOrderDate(Long time) {
        if (time == null || time == 0L) return " ";
        return getProjectTime(time, DATE_DMY_ORDER);
    }

    /**
     * 描述：获取milliseconds表示的日期时间的字符串.
     *
     * @param milliseconds the milliseconds
     * @param format       格式化字符串，如："yyyy-MM-dd HH:mm:ss"
     * @return String 日期时间字符串
     */
    public static String getProjectTime(long milliseconds, String format) {
        if (milliseconds == 0L) {
            return "";
        }
        Locale locale = LanguageUtils.Companion.getLanguageLocale();
        String thisDateTime = null;
        try {
            SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat(format, locale);
//            mSimpleDateFormat.setTimeZone(TimeZone.getTimeZone("France/Paris"));
//            mSimpleDateFormat.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
            thisDateTime = mSimpleDateFormat.format(milliseconds);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return thisDateTime;
    }

    /**
     * 描述：获取milliseconds表示的日期时间的字符串.
     *
     * @param date   the Date
     * @param format 格式化字符串，如："yyyy-MM-dd HH:mm:ss"
     * @return String 日期时间字符串
     */
    public static String getProjectTime(Date date, String format) {
        Locale locale = LanguageUtils.Companion.getLanguageLocale();
        String thisDateTime = null;
        try {
            SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat(format, locale);
            thisDateTime = mSimpleDateFormat.format(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return thisDateTime;
    }


    /**
     * 描述：String类型的日期时间转化为Date类型.
     *
     * @param strDate String形式的日期时间
     * @param format  格式化字符串，如："yyyy-MM-dd HH:mm:ss"
     * @return Date Date类型日期时间
     */
    public static Date getDateByFormat(String strDate, String format) {
        Locale locale = LanguageUtils.Companion.getLanguageLocale();
        SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat(format, locale);//Locale.getDefault()
        Date date = null;
        try {
            date = mSimpleDateFormat.parse(strDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * String 转换成Long 时间数据
     *
     * @param strDate String形式的日期时间
     * @param format  格式化字符串，如："yyyy-MM-dd HH:mm:ss"
     */
    public static long getLongByFormat(String strDate, String format) {
        SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat(format, Locale.getDefault());
        Date date = null;
        try {
            date = mSimpleDateFormat.parse(strDate);
            return date.getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 根据日期参数 生成时间
     *
     * @param year 年
     */
    public static long getLongByNumBer(int year, int mouth, int day, int h, int m, int s) {
        Calendar cd = Calendar.getInstance();
        try {
            cd.set(year, mouth, day, h, m, s);
            cd.getTimeInMillis();
            return cd.getTimeInMillis();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 根据日期参数 生成时间
     *
     * @param year 年
     * @return 2019-03-31
     */
    public static String getStringByNumBer(int year, int mouth, int day) {
        StringBuilder sb = new StringBuilder();
        sb.append(year);
        sb.append("-");
        sb.append(StrUtil.format2LenStr(mouth));
        sb.append("-");
        sb.append(StrUtil.format2LenStr(day));
        return sb.toString();
    }

    /**
     * 描述：Date类型转化为String类型.
     *
     * @param date   the date
     * @param format the format
     * @return String String类型日期时间
     */
    public static String getStringByFormat(Date date, String format) {
        SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat(format, Locale.getDefault());
        String strDate = null;
        try {
            strDate = mSimpleDateFormat.format(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return strDate;
    }

    /**
     * 描述：获取指定日期时间的字符串,用于导出想要的格式.
     *
     * @param strDate String形式的日期时间，必须为yyyy-MM-dd HH:mm:ss格式
     * @param format  输出格式化字符串，如："yyyy-MM-dd HH:mm:ss"
     * @return String 转换后的String类型的日期时间
     */
    public static String getStringByFormat(String strDate, String format) {
        String mDateTime = null;
        try {
            Calendar c = new GregorianCalendar();
            SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat(DATEFORMATYMDHMS, Locale.getDefault());
            c.setTime(mSimpleDateFormat.parse(strDate));
            SimpleDateFormat mSimpleDateFormat2 = new SimpleDateFormat(format, Locale.getDefault());
            mDateTime = mSimpleDateFormat2.format(c.getTime());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return mDateTime;
    }

    /**
     * 描述：获取milliseconds表示的日期时间的字符串.
     *
     * @param milliseconds the milliseconds
     * @param format       格式化字符串，如："yyyy-MM-dd HH:mm:ss"
     * @return String 日期时间字符串
     */
    public static String getStringByFormat(long milliseconds, String format) {
        String thisDateTime = "";
        try {
            SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat(format, Locale.getDefault());
            thisDateTime = mSimpleDateFormat.format(milliseconds);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return thisDateTime;
    }


    /**
     * 描述：获取表示当前日期时间的字符串.
     *
     * @param format 格式化字符串，如："yyyy-MM-dd HH:mm:ss"
     * @return String String类型的当前日期时间
     */
    public static String getCurrentDate(String format) {
        String curDateTime = null;
        try {
            SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat(format, Locale.getDefault());
            Calendar c = new GregorianCalendar();
            curDateTime = mSimpleDateFormat.format(c.getTime());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return curDateTime;

    }

    /**
     * 获取当前默认的时区
     */
    public static String getDefaultTimeZone() {
        TimeZone tz = TimeZone.getDefault();
        String zone = tz.getDisplayName(false, TimeZone.SHORT);
        if (null != zone && zone.length() > 0) {
            if (zone.endsWith(":00"))
                zone = zone.replace(":00", "");
            final char c = zone.charAt(zone.length() - 2);
            if ("0".equals(String.valueOf(c))) {
                return zone.replace(String.valueOf(c), "");
            }
        }
        return zone;
    }

    /**
     * 获取当前默认的时区
     * 强制GMT
     */
    public static String getLocaleTimeZone() {
        //获取系统的时间  ;时区类有夏令时的差异 TimeZone.getDefault().getRawOffset() / (3600 * 1000);
//        int _t = TimeZone.getDefault().getOffset(System.currentTimeMillis()) / (3600 * 1000);
//        String timezone = String.valueOf(_t);
//        timezone = "GMT" + timezone;
        //Calendar 计算方式
        Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT"), Locale.getDefault());
        Date currentLocalTime = calendar.getTime();
        DateFormat date = new SimpleDateFormat("z", Locale.getDefault());
        String timeZone = date.format(currentLocalTime);
        if (null != timeZone && timeZone.length() > 0) {
            if (timeZone.endsWith(":00"))
                timeZone = timeZone.replace(":00", "");
            return timeZone;
        }
        return "";
    }


    /**
     * 根据不同时区，转换时间
     *
     * @param date
     * @return
     */
    public static Date transformTime(Date date, TimeZone oldZone, TimeZone newZone) {
        Date finalDate = null;
        if (date != null) {
            int timeOffset = oldZone.getOffset(date.getTime()) - newZone.getOffset(date.getTime());
            finalDate = new Date(date.getTime() - timeOffset);
        }
        return finalDate;
    }


    /**
     * 两个时间相差距离多少天多少小时多少分多少秒
     *
     * @param str1 时间参数 1 格式：1990-01-01 12:00:00
     * @param str2 时间参数 2 格式：2009-01-01 12:00:00
     * @return long[] 返回值为：{天, 时, 分, 秒}
     */
    public static long[] getDistanceTimes(String str1, String str2) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
        Date one;
        Date two;
        long day = 0;
        long hour = 0;
        long min = 0;
        long sec = 0;
        try {
            one = df.parse(str1);
            two = df.parse(str2);
            long time1 = one.getTime();
            long time2 = two.getTime();
            long diff;
            if (time1 < time2) {
                diff = time2 - time1;
            } else {
                diff = time1 - time2;
            }
            day = diff / (24 * 60 * 60 * 1000);
            hour = (diff / (60 * 60 * 1000) - day * 24);
            min = ((diff / (60 * 1000)) - day * 24 * 60 - hour * 60);
            sec = (diff / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        long[] times = {day, hour, min, sec};
        return times;
    }

    /**
     * 两个时间相差距离多少天多少小时多少分多少秒
     *
     * @param str1 时间参数 1 格式：1990-01-01 12:00:00
     * @param str2 时间参数 2 格式：2009-01-01 12:00:00
     * @return String 返回值为：xx天xx小时xx分xx秒
     */
    public static String getDistanceTime(String str1, String str2) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
        Date one;
        Date two;
        long day = 0;
        long hour = 0;
        long min = 0;
        long sec = 0;
        try {
            one = df.parse(str1);
            two = df.parse(str2);
            long time1 = one != null ? one.getTime() : 0;
            long time2 = two != null ? two.getTime() : 0;
            long diff;
            if (time1 < time2) {
                diff = time2 - time1;
            } else {
                diff = time1 - time2;
            }
            day = diff / (24 * 60 * 60 * 1000);
            hour = (diff / (60 * 60 * 1000) - day * 24);
            min = ((diff / (60 * 1000)) - day * 24 * 60 - hour * 60);
            sec = (diff / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return day + " : " + hour + " : " + min + " : " + sec;
    }

    /**
     * 两个时间之间相差距离多少天
     *
     * @param str1 时间参数 1：
     * @param str2 时间参数 2：
     * @return 相差天数
     */
    public static long getDistanceDays(String str1, String str2) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
        Date one;
        Date two;
        long days = 0;
        try {
            one = df.parse(str1);
            two = df.parse(str2);
            long time1 = one.getTime();
            long time2 = two.getTime();
            long diff;
            if (time1 < time2) {
                diff = time2 - time1;
            } else {
                diff = time1 - time2;
            }
            days = diff / (1000 * 60 * 60 * 24);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return days;
    }

    //*******************************业务相关方法***********************************************

    /**
     * 转换时间格式
     *
     * @param timeMs 毫秒
     */
    public static String stringForTime(int timeMs) {
        if (timeMs <= 0 || timeMs >= 24 * 60 * 60 * 1000) {
            return "00";
        }
        int totalSeconds = timeMs / 1000;
        int seconds = totalSeconds % 60;
        int minutes = (totalSeconds / 60) % 60;
        int hours = totalSeconds / 3600;
        StringBuilder stringBuilder = new StringBuilder();
        Formatter mFormatter = new Formatter(stringBuilder, Locale.getDefault());
        if (hours > 0) {
            return mFormatter.format("%d:%02d:%02d", hours, minutes, seconds).toString();
        } else if (minutes > 0) {
            return mFormatter.format("%02d:%02d", minutes, seconds).toString();
        } else {
            return mFormatter.format("%02d", seconds).toString();
        }
    }


    /**
     * 转时间
     *
     * @param time 毫秒
     */
    public static String time2textTime(long time) {
        long day = 0;
        long hour = 0;
        long min = 0;
        long sec = 0;

        day = time / (24 * 60 * 60 * 1000);
        hour = (time / (60 * 60 * 1000) - day * 24);
        min = ((time / (60 * 1000)) - day * 24 * 60 - hour * 60);
        sec = (time / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
//        if (day>0)
//            return time0format(day) + ":" + time0format(hour) + ":" + time0format(min) + ":" + time0format(sec);
//        else
//            return time0format(hour) + ":" + time0format(min) + ":" + time0format(sec);
        return time0format(day) + ":" + time0format(hour) + ":" + time0format(min) + ":" + time0format(sec);
    }

    /**
     * 转时间 hh:mm
     *
     * @param time s
     */
    public static String time2textHrMin(long time) {
        long hour = 0;
        long min = 0;
        hour = (time / (60 * 60));
        min = ((time / 60) - hour * 60);

        return time0format(hour) + ":" + time0format(min);
    }

    /**
     * 时间戳转换
     *
     * @param time 毫秒
     * @return
     */
    public static long[] time2ArrayTime(long time) {
        long day = 0;
        long hour = 0;
        long min = 0;
        long sec = 0;

        day = time / (24 * 60 * 60 * 1000);
        hour = (time / (60 * 60 * 1000) - day * 24);
        min = ((time / (60 * 1000)) - day * 24 * 60 - hour * 60);
        sec = (time / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);

        long[] times = {day, hour, min, sec};
        return times;
    }


    /**
     * 时间戳转换
     *
     * @param time 毫秒
     * @return
     */
    public static String[] time2ArrayStr(long time) {
        long day = 0;
        long hour = 0;
        long min = 0;
        long sec = 0;

        day = time / (24 * 60 * 60 * 1000);
        hour = (time / (60 * 60 * 1000) - day * 24);
        min = ((time / (60 * 1000)) - day * 24 * 60 - hour * 60);
        sec = (time / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);

        String[] times = {time0format(day), time0format(hour), time0format(min), time0format(sec)};
        return times;
    }



    /**数字补零*/
    public static String time0format(long time) {
        String str = time + "";
        str = str.length() == 1 ? "0" + str : str;
        return str;
    }


}
